package com.xc.h05.controller.wutao;

import com.github.pagehelper.PageInfo;
import com.xc.h05.entity.Consult;
import com.xc.h05.entity.Coursetype;
import com.xc.h05.entity.LevelAndTypes;
import com.xc.h05.service.wutao.ConsultService;
import com.xc.h05.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/consult2")
public class ConsultController2 {
    @Autowired
    private ConsultService consultService;
//查询所有
    @GetMapping("/findAll")
    public List<Consult> findAll(){
        return consultService.findAll();
    }
//增加
    @PostMapping("/add")
    public R add(@RequestBody Consult consult) {
        int add = consultService.add(consult);
        if (add != 0) {
            return R.ok(consult);
        } else {
            return R.fail("添加失败");
        }
    }
//根据ID删除
    @DeleteMapping("/delete")
    public int delete(int id){
        return consultService.delete(id);
    }
    //更改
    @PutMapping("/update")
    public int update(@RequestBody Consult consult){
        return consultService.update(consult);
    }
    //分页查询所有
    @GetMapping("/getAll")
    public R getAll(
            @RequestParam(value = "pageNum", required = false, defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", required = false, defaultValue = "10") int pageSize,
            Consult consult
    ) {
        PageInfo<Consult> pageInfo = consultService.findByPage(pageNum, pageSize,consult);
        return R.ok(pageInfo);
    }
    //根据id批量删除
    @PostMapping("/deleteByIds")
    public R deleteByIds(@RequestBody java.util.List<Long> ids) {
        int i = consultService.deleteByIds(ids);
        if (i >= 0) {
            return R.ok("批量删除成功");
        }else return R.fail ("批量删除失败");
    }
    //根据id查询
    @GetMapping("/findById2")
    public R findById2(int id){
        Consult consult = consultService.findById(id);
        return R.ok(consult);
    }
    //根据咨询师表名获取擅长领域对应信息
    @GetMapping("/ConslutWithSpecialty")
    public List<Consult> ConslutWithSpecialty(){
        List<Consult> consults = consultService.ConslutWithSpecialty();
        return consults;
    }

    //根据cid和擅长领域查询
    @PostMapping("/findByCid")
    public R findByCid(@RequestBody LevelAndTypes levelAndTypes){//int cid,@RequestBody List<Integer> list
        /*int cid = 1;
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);*/
        List<Consult> list1 = consultService.findByCid(levelAndTypes.getLevel(), levelAndTypes.getTypes());
        return R.ok(list1);
    }
    // 根据擅长领域id查询对应的咨询师
    @GetMapping("/findBySpecialtyId")
    public R findBySpecialtyId(int pageNum,int pageSize,int id){
        PageInfo<Consult> pageInfo = consultService.findBySpecialtyId(pageNum, pageSize, id);
        return R.ok(pageInfo);
    }

}
